<?php
/*
 * @author : Pham Huu Sam
 * @phone : 0922.926.269
 */
class Trangtinh extends CI_Controller{
    protected $_gallery_path = "";
    protected $_gallery_url = "";
	public $model_result;
	public $countloop = 0;
    public $mPublicTheLoai = '0';

    public function  __construct() {
        parent::__construct();
		require_once(APPPATH. '/functions/application.php'); 
        $this->load->helper("url");
        $this->load->library("admin_layout"); // Sử dụng thư viện layout
        $this->admin_layout->setLayout("layout/admin"); // load file layout chính (view/layout/frontend.php)
        $this->load->Model("Mtrangtinhs");
        $this->load->Model("M_theloai");
        $this->load->library('pagination'); 
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->helper("text");
	
    }

    // Controller mặc định cho trang chủ
    public function index(){
        
        $data['title']         = "Danh sách tin tức";
        $config['base_url']    = base_url('admin/trangtinh/index/'); // xác định trang phân trang 
        $config['total_rows']  = $this->Mtrangtinhs->count_all(); // xác định tổng số record 
        $config['per_page']    = 10; // xác định số record ở mỗi trang
        $config['uri_segment'] = 4; // xác định segment chứa page number 
        
       
        $this->pagination->initialize($config); 
        $data['data']=$this->Mtrangtinhs->list_with_page($config['per_page'],$this->uri->segment($config['uri_segment'])); 
        $data['navigation']['title'] = 'Quản lý tin tức';
		$data['navigation']['active']= 'Danh sách tin';
        $this->admin_layout->view("admin/trangtinh",$data);
    }

    public function edit($id) {
        
        $data['msg']                  = '<p style="font-weight: bold">Lưu ý: Khi Sửa mà không thành công sẽ load lại dữ liệu cũ.<p>';
        $data['title']                = 'Sửa Tin Tức';
        $data['navigation']['title']  = 'Quản lý tin tức';
        $data['navigation']['active'] = 'Sửa Tin Tức';
        $data['data']                 =$this->Mtrangtinhs->GetTinTuc($id);
        $this->mPublicTheLoai         = $data['data']-> id_TheLoai;        
        $data['dataListNews']         = $this->Mtrangtinhs->listall();
        $result                       = array();
		
        $config = array(
            array(
                 'field' => 'txtTieuDe', 
                 'label' => 'Tiêu Đề', 
                 'rules' => 'required'
            ),

           array(
                 'field' => 'txtKeyWord', 
                 'label' => 'Từ Khóa', 
                 'rules' => 'required'
            )
        );
		
        $this->form_validation->set_rules($config);
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        if ($this->form_validation->run() == FALSE)
        {
           $this->admin_layout->view("admin/editTrangTinh",$data);
        } else {
            if(isset($_REQUEST["save"])) {
                $config = array(
                    'upload_path'   => 'public/frontend/upload',
                    'allowed_types' => '*',
                    'max_size'      => '2000');

                
                $this->load->library("upload",$config);
                //$slug = mb_strtolower(url_title(removesign($this->input->post('productKeyWord'))));
                if (empty($_FILES['fileImage']['name'])) {
                    //echo "không file";
                    $data_edit = array(
                        'TieuDe'     => $this->input->post('txtTieuDe'),
                        'NoiDung'    => $this->input->post('txtNoiDung',FALSE),
                        'Keywords'   => $this->input->post('txtKeyWord'),
                        'AnHien'     => $this->input->post('radioVisible'),
                        'id_user'    => $this->session->userdata('NguoiDung_id'),
                        'LastUpdate' => date('m/d/Y h:i:s a', time())
                    );
                    $this -> Mtrangtinhs -> Update($data_edit, $id);
                } else {
                    if(!$this->upload->do_upload("fileImage")){
                        $arrayErrors = array($this->upload->display_errors());
                        $data['msg'] =  "<div class='error'>".$arrayErrors[0]."</div>";
                        $this->admin_layout->view("admin/editTrangTinh",$data);
                    }else{
                        $image_data = $this->upload->data();
                        $config = array(
                            "source_image"    => $image_data['full_path'],
                            "new_image"       => "public/frontend/images/photos",
                            "maintain_ration" => true,
                            "width"           => '140',
                            "height"          => "105"
                        )
                        ;
                        $this->load->library("image_lib",$config);
                        $this->image_lib->resize();

                        $data_edit = array(
                            'TieuDe'     => $this->input->post('txtTieuDe'),
                            'urlHinh'    => $image_data['file_name'],
                            'NoiDung'    => $this->input->post('txtNoiDung', FALSE),
                            'Keywords'   => $this->input->post('txtKeyWord'),
                            'AnHien'     => $this->input->post('radioVisible'),
                            'id_user'    => $this->session->userdata('NguoiDung_id'),
                            'id_TheLoai' => $this->input->post('cbChuyenMuc'),
                            'LastUpdate' => date('m/d/Y h:i:s a', time())
                        );
                        
                        $this -> Mtrangtinhs -> Update($data_edit, $id);
                    }

                }
                redirect('admin/trangtinh');
            } else {
                $this->admin_layout->view("admin/editTrangTinh",$data);
            }
        }
    }
}
?>
